﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.IO;
using Com.CodePlex.SPC3.SPbAM.Data;
using Com.CodePlex.SPC3.SPbAM.Controller;
using Com.CodePlex.SPC3.SPbAM.Model;
using System.Windows.Input;
using System.Net.NetworkInformation;
using System.Text;
//using Com.CodePlex.SPC3.SPbAM.View; //should not reference this http://social.msdn.microsoft.com/Forums/is/wpf/thread/3bced3ea-4a97-4456-bfe5-828289bcf971

namespace Com.CodePlex.SPC3.SPbAM.ViewModel {

	public class ActionFolderViewModel : LazyChildrenViewModel {

		private ServerViewModel parent;
		private string path;

		public ActionFolderViewModel(string path, ServerViewModel parent) {
			if (path == null) {
				throw new ArgumentNullException("path");
			}
			if (parent == null) {
				throw new ArgumentNullException("parent");
			}
			this.path = path;
			this.parent = parent;
			DisplayName = new DirectoryInfo(path).Name;
			Mediator.Register(this);
			//note: lazy load: dont do more stuff here
		}

		#region Commands

		private ICommand hidecommand;
		public ICommand HideCommand { get { return hidecommand ?? (hidecommand = new RelayCommand(param => Hide())); } }

		private IEnumerable<CommandViewModel> treecommands;
		public IEnumerable<CommandViewModel> TreeCommands {
			get {
				if (treecommands == null) {
					treecommands = new List<CommandViewModel>() {
						new CommandViewModel("Hide", HideCommand)
					};
				}
				return treecommands;
			}
		}

		#endregion

		#region Display Properties

		private bool isselected;
		public bool IsSelected {
			get { return isselected; }
			set {
				if (value != isselected) {
					isselected = value;
					OnPropertyChanged(() => IsSelected);
				}
			}
		}

		public string ImageSource {
			get { return "pack://application:,,,/Resources/Images/Folder.jpg"; }
		}

		#endregion

		public void Hide() { //todo: should really be an observable property on ServerViewModel that all sub nodes read
			int i = 0;
		}

		protected override void Load() {
			new DirectoryInfo(path).GetDirectories()
			.ToList()
			.Where(i => (i.Attributes & FileAttributes.Hidden) == 0)
			.ToList()
			.ForEach(i => {
				Children.Add(new ActionFolderViewModel(i.FullName, parent));
			});
			new DirectoryInfo(path).GetFiles()
				.ToList()
				.Where(i => i.Extension == ".ps1")
				.ToList()
				.ForEach(i => {
					Children.Add(new ActionViewModel(i.FullName, parent));
			});
		}
	}
}